package com.shijunhao.graduation.service.business.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shijunhao.graduation.domain.dto.DiscussDto;
import com.shijunhao.graduation.domain.entity.*;
import com.shijunhao.graduation.domain.vo.CommonDiscussVo;
import com.shijunhao.graduation.domain.vo.CurrentUserVo;
import com.shijunhao.graduation.domain.vo.DiscussVo;
import com.shijunhao.graduation.enums.DictCode;
import com.shijunhao.graduation.enums.TableName;
import com.shijunhao.graduation.exception.BusinessException;
import com.shijunhao.graduation.mapper.BusinessForumDiscussMapper;
import com.shijunhao.graduation.properties.RoleParams;
import com.shijunhao.graduation.service.business.IBusinessForumDiscussService;
import com.shijunhao.graduation.service.business.IBusinessForumService;
import com.shijunhao.graduation.service.business.IBusinessUserGoodService;
import com.shijunhao.graduation.service.business.IBusinessUserInfoService;
import com.shijunhao.graduation.service.system.ISystemDictService;
import com.shijunhao.graduation.service.system.impl.BaseServiceImpl;
import com.shijunhao.graduation.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shijunhao
 * @since 2022-04-16
 */
@Service
public class BusinessForumDiscussServiceImpl extends BaseServiceImpl<BusinessForumDiscussMapper, BusinessForumDiscuss> implements IBusinessForumDiscussService {
    @Resource
    private IBusinessUserInfoService businessUserInfoService;
    @Resource
    private IBusinessUserGoodService goodService;
    @Resource
    private ISystemDictService systemDictService;
    @Resource
    private IBusinessForumService businessForumService;

    @Override
    public IPage<DiscussVo> page(IPage<BusinessForumDiscuss> curPage, Long fid) {
        Page<BusinessForumDiscuss> page = page(curPage, Wrappers.lambdaQuery(BusinessForumDiscuss.class)
                .eq(BusinessForumDiscuss::getFid, fid)
                .isNull(BusinessForumDiscuss::getParentId));
        return page.convert(this::toVo);
    }

    @Override
    public DiscussVo toVo(BusinessForumDiscuss entity) {
        DiscussVo discussVo = new DiscussVo();
        CommonDiscussVo commonDiscussVo = new CommonDiscussVo();
        commonDiscussVo.setNid(entity.getFid());
        BeanUtils.copyProperties(entity, commonDiscussVo);
        //设置初始信息
        discussVo.setDiscuss(commonDiscussVo);
        //设置评论人信息
        BusinessUserInfo userInfo = businessUserInfoService.getOne(Wrappers.lambdaQuery(BusinessUserInfo.class).eq(BusinessUserInfo::getUid, entity.getCreateBy()));
        discussVo.setUserInfo(userInfo);
        //设置当前用户是否点赞信息
        SystemDictItem table = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), TableName.BUSINESS_FORUM_DISCUSS.getLabel());
        BusinessUserGood userGood = goodService.getOneHidden(entity.getId(), table.getDictValue(), true, false);
        if(userGood != null){
            discussVo.setGood(true);
        }
        //设置子评论信息
        if (entity.getParentId() == null) {
            Page<BusinessForumDiscuss> page = page(PageUtils.initPage(), Wrappers.lambdaQuery(BusinessForumDiscuss.class).eq(BusinessForumDiscuss::getParentId, entity.getId()));
            Page<DiscussVo> vos = (Page<DiscussVo>) page.convert(this::toVo);
            discussVo.setChildDiscuss(vos);
        }
        return discussVo;
    }

    @Override
    public Long insert(DiscussDto dto) {
        BusinessForumDiscuss entity = new BusinessForumDiscuss();
        BeanUtils.copyProperties(dto, entity);
        entity.setGoods(0);
        entity.setFid(dto.getNid());

        if(dto.getParentId() == null){
            BusinessForumDiscuss businessForumDiscusses = getOne(Wrappers.lambdaQuery(BusinessForumDiscuss.class).eq(BusinessForumDiscuss::getFid, entity.getFid())
                    .orderByDesc(BusinessForumDiscuss::getFloor)
                    .last("limit 1"));
            Integer floor = businessForumDiscusses == null ? 1 : businessForumDiscusses.getFloor() + 1;
            entity.setFloor(floor);
        }

        boolean b = insertOne(entity);
        if (b)
            return entity.getId();
        else
            throw new BusinessException("插入评论失败");
    }

    @Override
    public boolean delete(Long id) {
        //判断是否是管理员或该评论的文章或论坛作者是否为本人
        CurrentUserVo user = businessUserInfoService.getCurrentOrgUser();

        BusinessForumDiscuss dis = this.getById(id);
        BusinessForum forum = businessForumService.getById(dis.getFid());
        if(!forum.getCreateBy().equals(user.getUser().getId()) && !RoleParams.ADMIN.equals(user.getRole())){
            throw new BusinessException("无删除权限");
        }else{
            return deleteById(id);
        }
    }

}
